import React, { useState } from 'react';
import { View, TextInput, Button, Alert, StyleSheet } from 'react-native';
import axios from 'axios';
import AsyncStorage from '@react-native-async-storage/async-storage';
import Toast from 'react-native-toast-message';
import { useNavigation } from '@react-navigation/native';

export default function ChangePasswordScreen({ route }) {
    const navigation = useNavigation();
    const [password, setPassword] = useState('');
    const phone = JSON.stringify(route.params.phone)

    const changePassword = () => {
        // 在这里处理修改密码的逻辑
        console.log(`手机号: ${phone}\n新密码: ${password}`);
        // 假设你的修改密码API是 'http://47.111.227.58:3000/login/updataUser'
        axios.post('http://47.111.227.58:3000/login/updataUser', {
            data: {
                password: password,
                phone: phone
            }
        })
            .then(function (res) {
                console.log(res.data);
                if (res.data.code == 200) {
                    Toast.show({
                        type: 'success',
                        text1: res.data.data.msg,
                    });
                    // 假设你的登录页面是 'LoginScreen'
                    navigation.navigate('Login');
                } else {
                    Toast.show({
                        type: 'error',
                        text1: res.data.data.msg,
                    });
                }
            })
            .catch(function (error) {
                console.log(error);
            });
    };

    return (
        <View style={{ flex: 1, justifyContent: 'center', padding: 16, backgroundColor: '#FFF' }}>
            <TextInput
                style={{ height: 40, borderColor: '#808080', borderWidth: 1, marginBottom: 16, borderRadius: 5, paddingLeft: 10 }}
                onChangeText={setPassword}
                value={password}
                placeholder="请输入新的密码"
            />
            <Button
                title="确定修改"
                onPress={changePassword}
                color="#EB7A76"
            />
        </View>
    );
}
